package yxy.game.pm2.active.command.belovedhero;

import yxy.cherry.data.bean.actfavoritewarlordshop;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心仪武将活动;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;

import java.util.ArrayList;
import java.util.Map;

/****************************************
 * @name CMD_Active_心仪武将_ActBelovedHeroShopInfo
 * @description 心仪武将活动商城信息
 * @author 梁健彬
 * @since 2022-07-8
 ***************************************/
@MessageType(proto = Command.ActBelovedHeroShopInfo, classtype = ActiveMessage.ActBelovedHeroShopInfoRequest.class)
public class CMD_Active_心仪武将_ActBelovedHeroShopInfo extends Command<ActiveMessage.ActBelovedHeroShopInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActBelovedHeroShopInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActBelovedHeroShopInfoResponse.Builder response = ActiveMessage.ActBelovedHeroShopInfoResponse.newBuilder();

        try {
            ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(playercode, false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            ActBelovedHeroHandle heroHandle = new ActBelovedHeroHandle();
            //商品表
            Map<String, actfavoritewarlordshop> map = ActDataCenter.getAllData(actfavoritewarlordshop.class, playercode);

            //返回商品信息
            ArrayList<ActiveMessage.ActBelovedHeroShop> list = heroHandle.getFavouritePackage(am, map, 1);

            response.addAllShop(list);


            //开始结束时间
            Timeline timeline = am.getTimeline();
            long startTime = timeline.getStartTime().getMillis();
            long endTime = timeline.getEndTime().getMillis();

            //返回
            response.setBeginTime(startTime);
            response.setEndTime(endTime);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, ActiveMessage.ActBelovedHeroShopInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
